generate_isotopes.js ➔ isotopePrefix   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 4
rs 10
nop 1
1
/* eslint-env node */
2
/*jslint node: true */
3
'use strict';
4
5
let jsonfile = require('jsonfile');
6
7
let resources = jsonfile.readFileSync('build/data/resources.json');
8
let elements = jsonfile.readFileSync('build/data/elements.json');
9
let radioisotopes = [];
10
11
let abundance = 0;
12
for (let element in elements) {
13
  abundance += elements[element].abundance;
14
  elements[element].includes = elements[element].includes || [];
15
  let exotic = 'x' + element;
16
  elements[element].exotic = exotic;
17
18
  resources[exotic] = {};
19
  resources[exotic].elements = {};
20
  resources[exotic].elements[element] = 1;
21
  resources[exotic].type = ['exotic'];
22
23
  let isotopes = elements[element].isotopes;
24
  let ratioSum = 0;
25
  let mainIsotope = [0, ''];
26
  for (let isotope in isotopes) {
27
    resources[isotope] = {};
28
29
    // https://en.wikipedia.org/wiki/Nuclear_binding_energy#Semiempirical_formula_for_nuclear_binding_energy
30
    let p = 938272081;
31
    let n = 939565413;
32
    let e = 511000;
33
34
    let Z = elements[element].number;
35
    let A = parseInt(isotope, 10);
36
    let N = A - Z;
37
    let calculatedEnergy = p*Z+n*N+e*Z;
38
    let calculatedBinding = 14-(13/Math.pow(A,1/3))-(0.585*Z*Z/Math.pow(A,4/3))-(19.3*Math.pow(N-Z,2)/Math.pow(A,2));
39
    if(Z%2 === 0 && N%2 === 0){
40
      calculatedBinding += 33/Math.pow(A,7/4)
41
    }
42
    if(Z%2 === 1 && N%2 === 1){
43
      calculatedBinding -= 33/Math.pow(A,7/4)
44
    }
45
    calculatedBinding *= A*1e6;
46
    calculatedBinding = Math.round(calculatedBinding);
47
    // the experimental formula fails for some small isotopes
48
    // so we just introduce the values by hand
49
    if(typeof isotopes[isotope].binding_energy !== 'undefined'){
50
      calculatedBinding = isotopes[isotope].binding_energy;
51
    }
52
53
    resources[isotope].energy = calculatedEnergy-calculatedBinding;
54
    resources[isotope].binding_energy = calculatedBinding;
55
    resources[isotope].elements = {};
56
    resources[isotope].elements[element] = 1;
57
    resources[isotope].html = isotopePrefix(isotope) + element;
58
    resources[isotope].decay = isotopes[isotope].decay;
59
    resources[isotope].type = ['isotope'];
60
61
    delete isotopes[isotope].decay;
62
    delete isotopes[isotope].binding_energy;
63
64
    if (elements[element].includes.indexOf(isotope) === -1) {
65
      elements[element].includes.push(isotope);
66
    }
67
68
    ratioSum += isotopes[isotope].ratio;
69
    if (isotopes[isotope].ratio > mainIsotope[0]) {
70
      mainIsotope[0] = isotopes[isotope].ratio;
71
      mainIsotope[1] = isotope;
72
    }
73
  }
74
  let difference = 1 - ratioSum;
75
  if (Math.abs(difference) > 1e-7 && elements[element].abundance > 0) {
76
    throw new Error('Ratios add up to '.concat(1 - difference, ' for ', element));
77
  }
78
  elements[element].main = mainIsotope[1];
79
}
80
let difference = 1 - abundance;
81
if (Math.abs(difference) > 1e-7) {
82
  throw new Error('Abundance adds up to '.concat(1 - difference));
83
}
84
85
for (let resource in resources) {
86
  if (typeof resources[resource].decay !== 'undefined') {
87
    radioisotopes.push(resource);
88
  }
89
}
90
91
function isotopePrefix(isotope) {
92
  let prefix = isotope.replace(/(^\d+)(.+$)/i, '$1');
93
  return '<sup>' + prefix + '</sup>';
94
}
95
96
jsonfile.writeFileSync('build/data/resources.json', resources, {
97
  spaces: 2
98
});
99
jsonfile.writeFileSync('build/data/elements.json', elements, {
100
  spaces: 2
101
});
102
jsonfile.writeFileSync('build/data/radioisotopes.json', radioisotopes, {
103
  spaces: 2
104
});
105